package com.tamartech.greendb.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.tamartech.greendb.data.Election;


public class ElectionDAO extends DAO
{
   public Election save (Election a) 
         throws Exception
   {
      try
      {
         begin();
         getSession().save(a);
         commit();
         return a;
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not create Election", e);
      }
   }


//   public void update(Election a) throws Exception 
//   {
//      try 
//      {
//         begin();
//         getSession().update(a);
////         getSession().save(a);
//         commit();
//      } 
//      catch (HibernateException e) 
//      {
//         rollback();
//         throw new Exception("Could not save Election", e);
//      }
//   }

   public Election get (Long id) throws Exception 
   {
      try 
      {
         begin();
         Election a = (Election) getSession().load(Election.class, id);
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain Election " + id, e);
      }
   }
/*
   public Election get (String name) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Election where name = :name");
         q.setString ("name", name);
         Election a = (Election) q.uniqueResult();
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain Election " + name, e);
      }
   }
*/
   public Election getByDate (Date d, String type) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Election where date = :date and type = :type");
         q.setDate ("date", d);
         q.setString("type", type);

         Election found = (Election) q.uniqueResult();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain Election for date: " + d, e);
      }
   }
   
   @SuppressWarnings("unchecked")
   public List<Election> list() throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Election");
         List<Election> list = q.list();
         commit();
         return list;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not list the elections", e);
      }
   }

   
   public void delete(Election a)
      throws Exception
   {
      try
      {
         begin();
         getSession().delete(a);
         commit();
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not delete Election", e);
      }
   }
}
